//
//  ZGLEpisodeCell.m
//  Faker
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLEpisodeCell.h"
#import "ZGLSubDetModel.h"
#import <UIImageView+AFNetworking.h>

@interface ZGLEpisodeCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong) UILabel *dateLabel;

@end

@implementation ZGLEpisodeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _picImage  = [[UIImageView alloc] init];
        _titleLabel  =[[UILabel alloc] init];
        _dateLabel  =[[UILabel alloc] init];
        _backView = [[UIView alloc] init];
        
        [_backView addSubview:_picImage];
        [_backView addSubview:_titleLabel];
        [_backView addSubview:_dateLabel];
        [self.contentView addSubview:_backView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.backView setFrame:CGRectMake(ScaleX * 10, ScaleY * 10, ScaleX * 355, ScaleY * 370)];
    [self.titleLabel setFrame:CGRectMake(ScaleX * 10, ScaleX * 0, ScaleX *355, ScaleY * 40)];
    [self.picImage setFrame:CGRectMake(0, ScaleY * 40, ScaleX * 355, ScaleY * 300)];
    [self.dateLabel setFrame:CGRectMake(ScaleX * 10, ScaleY * 340, ScaleX * 355, ScaleY * 30)];
    self.titleLabel.textColor = artiColor;
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.dateLabel.textColor = [UIColor lightGrayColor];
    self.dateLabel.font = [UIFont systemFontOfSize:12];
    _backView.backgroundColor = foreColor;
}
- (void)setSubModel:(ZGLSubDetModel *)subModel {
    if (_subModel != subModel) {
        _subModel = subModel;
    }
    if (subModel.media.count == 0) {
        [_picImage setImage:[UIImage imageNamed:@"nopic"]];
    } else {
        [_picImage setImageWithURL:subModel.media[0][@"raw_url"] placeholderImage:[UIImage imageNamed:@"nopic"]];
    }
    _dateLabel.text = [NSString stringWithFormat:@"%@ %@", subModel.auther_name, [self getTimeDifferentWith:subModel.date]];
    _titleLabel.text = subModel.title;
   
}

- (NSString *)getNowTime {
    NSDate *senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    return locationString;
}
- (NSString *)getTimeDifferentWith:(NSString *)date {
    if (date.length == 0) {
        date = [self getNowTime];
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    NSDate *dateModel = [formatter dateFromString:date];
    NSDate *dateNow = [formatter dateFromString:[self getNowTime]];
    NSString *timeModel = [NSString stringWithFormat:@"%ld", (long)[dateModel timeIntervalSince1970]];
    NSString *timeNow = [NSString stringWithFormat:@"%ld", (long)[dateNow timeIntervalSince1970]];
    NSInteger time = ([timeNow integerValue] - [timeModel integerValue]) / 60;
    if (time > 60) {
        return [NSString stringWithFormat:@"%ld小时前",time / 60];
    } else {
        return [NSString stringWithFormat:@"%ld分钟前",time % 60];
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
